package com.neusoft.crm.controller;

import com.neusoft.crm.po.ClientInfo;
import com.neusoft.crm.po.ClientLinkMan;
import com.neusoft.crm.po.SaleChance;
import com.neusoft.crm.po.SysUser;
import com.neusoft.crm.service.IClientInfoService;
import com.neusoft.crm.service.ISaleChanceService;
import com.neusoft.crm.utils.GetDataStr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 销售机会管理前端接口类
 *
 * @author msprc
 */
@RestController
public class SaleChanceController {

    @Autowired
    private ISaleChanceService saleChanceService;

    @Autowired
    private IClientInfoService clientInfoService;

    /**
     * 销售机会列表
     *
     * @param saleChance 请求体：销售机会，对应状态属性非空
     * @return 符合要求的销售机会的列表
     */
    @RequestMapping("/selectSaleChanceByStatus")
    public List<SaleChance> selectSaleChanceByStatus(@RequestBody SaleChance saleChance) {
        return saleChanceService.selectSaleChanceByStatus(saleChance);
    }

    /**
     * 插入销售机会HTTP接口
     *
     * @param saleChance 请求体：销售机会
     * @return 受影响的行数：1为成功0为失败
     */
    @RequestMapping("/insertSaleChance")
    public int insertSaleChance(@RequestBody SaleChance saleChance) {
        return saleChanceService.insertSaleChance(saleChance);
    }

    /**
     * 更新销售机会
     *
     * @param saleChance 请求体：销售机会
     * @return 受影响的行数：1为成功0为失败
     */
    @RequestMapping("/updateSaleChance")
    public int updateSaleChance(@RequestBody SaleChance saleChance) {
        return saleChanceService.updateSaleChance(saleChance);
    }

    /**
     * 删除销售机会
     *
     * @param saleChance 请求体：销售机会
     * @return 受影响的行数：1为成功0为失败
     */
    @RequestMapping("/deleteSaleChance")
    public int deleteSaleChance(@RequestBody SaleChance saleChance) {
        return saleChanceService.deleteSaleChance(saleChance);
    }

    /**
     * 指派销售机会
     *
     * @param saleChance 请求体：销售机会
     * @return 受影响的行数：1为成功0为失败
     */
    @RequestMapping("/assignSalesChance")
    public int assignSalesChance(@RequestBody SaleChance saleChance) {
        saleChance.setChanceStatus(1);
        return saleChanceService.assignSalesChance(saleChance);
    }

    /**
     * 根据指派人ID查找对应的销售机会
     *
     * @param saleChance 带有指派人ID的销售机会对象
     * @return 销售机会列表
     */
    @RequestMapping("/selectAssignedSalesChance")
    public List<SaleChance> selectAssignedSalesChance(@RequestBody SaleChance saleChance) {
        return saleChanceService.selectAssignedSalesChance(saleChance);
    }

    /**
     * 通过id精确查询查找销售机会
     *
     * @param saleChance 销售机会
     * @return 机会列表
     */
    @RequestMapping("selectSalesChanceById")
    public SaleChance selectSalesChanceById(@RequestBody SaleChance saleChance) {
        return saleChanceService.selectSalesChanceById(saleChance);
    }

    /**
     * 通过模糊查询查找销售机会
     *
     * @param saleChance 销售机会
     * @return 机会列表
     */
    @RequestMapping("selectSaleChanceByTag")
    public List<SaleChance> selectSaleChanceByTag(@RequestBody SaleChance saleChance) {
        return saleChanceService.selectSaleChanceByTag(saleChance);
    }

    /**
     * 通过销售机会状态，state=3，返回系统用户
     *
     * @param saleChance 销售机会
     * @return 系统用户信息列表
     */
    @RequestMapping("selectSysUserNameBySaleChanceState")
    public List<SysUser> selectSysUserNameBySaleChanceState(@RequestBody SaleChance saleChance) {
        return saleChanceService.selectSysUserNameBySaleChanceState(saleChance);
    }

    /**
     * 完成开发计划
     *
     * @param saleChance 要完成的开发计划
     * @return 受影响的行数：1成功，0失败
     */
    @RequestMapping("/completeSaleChance")
    public int completeSaleChance(@RequestBody SaleChance saleChance) {

        String clientCode = GetDataStr.getDataStrWithCode();
        // Step1：在客户表中添加对应的客户
        clientInfoService.insertClient(setClientLinkManBySalePlan(saleChance, clientCode));

        // step2: 在联系人表中添加对应联系人
        ClientLinkMan clientLinkMan = new ClientLinkMan();
        clientLinkMan.setClientCode(clientCode);
        clientLinkMan.setClientLinkManName(saleChance.getChanceLinkman());
        clientLinkMan.setClientLinkManTel(saleChance.getChanceTel());
        clientLinkMan.setClientLinkManSex(0);
        clientLinkMan.setClientLinkManJob("");
        clientInfoService.insertClientLinkMan(clientLinkMan);

        return 1;
    }

    /**
     * 根据销售机会初始化客户对象
     *
     * @param saleChance 根据销售机会初始化一个客户对象
     * @param clientCode 客户编号
     * @return 客户
     */
    private ClientInfo setClientLinkManBySalePlan(SaleChance saleChance, String clientCode) {
        String noneInfoString = "暂无";
        ClientInfo clientInfo = new ClientInfo();
        clientInfo.setClientCode(clientCode);
        clientInfo.setClientName(saleChance.getChanceCustName());
        clientInfo.setClientAreaId(1);
        clientInfo.setClientCustId(saleChance.getSysUser().getUserId());
        clientInfo.setClientLevelId(1);
        clientInfo.setClientContentment(3);
        clientInfo.setClientCredit(3);
        clientInfo.setClientAddress(noneInfoString);
        clientInfo.setClientDakCode(noneInfoString);
        clientInfo.setClientTel(noneInfoString);
        clientInfo.setClientFax(noneInfoString);
        clientInfo.setClientNet(noneInfoString);
        clientInfo.setClientCorporation(noneInfoString);
        clientInfo.setClientBank(noneInfoString);
        clientInfo.setClientAccounts(noneInfoString);
        clientInfo.setClientState(1);
        return clientInfo;
    }
}
